require 'multisketchup/simple_logger'

begin

SKETCHUP_PATH = defined?(Sketchup) ? File.dirname(Sketchup.template_dir) : ".."

# =========================== Configuration Begins =========================== #
CONFIG_YAML = <<EOS
---
# File to load upon startup.  Comment out these lines to not load a file, but be
# aware that loading a file after startup will overwrite the MultiSketchup hooks.
file: building1.skp
path: #{SKETCHUP_PATH}

# Roles for Sketchup instances
# The first role should be called 'server'
roles:
- server:
    fov: 35
- client-left:
    fov: 35
    rotation: 90
- client-rear:
    fov: 35
    rotation: 180
- client-right:
    fov: 35
    rotation: 270

# TCP/IP host and port to use for DRb client/server Sketchup communication 
host: 127.0.0.1
port: 7777

# RoboRealm input (must configure local IP to correspond to UDPSocket::bind("<any>", xxx) )
robo_realm:
  enabled: true
  speed: 10
  sleep: 0.50
  local_ip: 127.0.0.1 # 129.21.252.19 # 129.21.227.234
  server_address: 127.0.0.1 # 129.21.252.72
  # 95 100 586 385
  xmin: -1.0 # 95
  xmax: 1.0 # 585
  ymin: -1.0 # 100
  ymax: 1.0 # 385
  max_box: 100
  yfac: 1
  xfac: 1
  

# Warp box to go back to Google Earth
warp:
  xmin: 99991400
  xmax: 99992000
  ymin: -1400
  ymax: -700
  uri: http://127.0.0.1:8080/close

EOS

# Path to Ruby installation that matches the version of Sketchup's embedded Ruby
# Type RUBY_VERSION into your console to see yours.
# (1.8.0 for Sketchup 6 on Win32, 1.8.4 for Sketchup 6 on OSX)

RUBY_ROOT = "C:/Program Files/Google/Google SketchUp 6/Plugins/multisketchup/ruby-1.8.0-win32-lib"

# ============================ Configuration Ends ============================ #

# Specify Ruby Paths
$: << File.join(File.dirname(__FILE__), '..') if __FILE__ == $0 # ./.. if invoked outside of Sketchup
RUBY_LIB = "#{RUBY_ROOT}/lib/ruby"
$: << "#{RUBY_LIB}/site_ruby/1.8"
$: << "#{RUBY_LIB}/site_ruby/1.8/i386-msvcrt"
$: << "#{RUBY_LIB}/site_ruby"
$: << "#{RUBY_LIB}/1.8"
$: << "#{RUBY_LIB}/1.8/i386-mswin32"

require 'yaml'
require 'multisketchup/magic_hash'
require 'open-uri'
require 'net/http'

# Read in config
config = YAML.load(CONFIG_YAML)


require 'multisketchup/roles'

# If we're outside of Sketchup, launch a CamBroker
unless defined?(Sketchup)
  broker = MultiSketchup::CamBroker.new(config)
  broker.run
  
  # Ensures that the DRb server thread completes its work before the process exits.
  DRb.thread.join
else
  broker = MultiSketchup::CamBroker::locate_broker(config)
  log "Broker is running.  Receiving role..."

  cam_role, cam_param = broker.pop_next_available_role

  
  log "Received role: #{cam_role}"

  Sketchup.open_file File.join(config.path, cam_role+'-'+config.file)
  
  if cam_role == 'server'
    cam_obj = MultiSketchup::CamServer.new(config)
    cam_obj.run
  else
    cam_obj = MultiSketchup::CamClient.new(config,cam_role)
    cam_obj.run # Sketchup.active_model.active_view.animation = cam_obj
  end

end
rescue Exception => e
  log "#{e.message}\n#{e.backtrace}"
end
